{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2cdca4d3",
   "metadata": {},
   "source": [
    "已知$x_1=(1,2,3), y_1=+1, x_2=(4,1,2), y_2=+1, x_3=(-1,2,-1), y_3=-1.$\n",
    "\n",
    "$min \\frac{1}{2}(14a_1^2+21a_2^2+6a_3^2+24a_1a_2+8a_2a_3)-(a_1+a_2+a_3). s.t. a_1+a_2-a_3=0, a_1 \\geq\\ 0, a_2 \\geq\\ 0, a_3 \\geq\\ 0. $\n",
    "\n",
    "$令f(a_1,a_2,a_3)=\\frac{1}{2}(14a_1^2+21a_2^2+6a_3^2+24a_1a_2+8a_2a_3)-(a_1+a_2+a_3)$\n",
    "\n",
    "转化为矩阵形式有\n",
    "$f(a_1,a_2,a_3)=\\frac{1}{2}\\left[ \\begin{matrix} a_1 & a_2 & a_3 \\end{matrix} \\right]\\left[ \\begin{matrix} 14 & 12 & 0 \\\\ 12 & 21 & 4 \\\\ 0 & 4 & 6 \\end{matrix} \\right]\\left[ \\begin{matrix} a_1 \\\\ a_2 \\\\ a_3 \\end{matrix} \\right]+\\left[ \\begin{matrix} a_1 & a_2 & a_3 \\end{matrix} \\right]\\left[ \\begin{matrix} -1 \\\\-1 \\\\ -1 \\end{matrix} \\right]$\n",
    "下面调用Octave的二次优化问题函数qp进行求解"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "59af290d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x =\r\n",
      "\r\n",
      "   0.1000\r\n",
      "        0\r\n",
      "   0.1000\r\n",
      "\r\n",
      "obj = -0.1000\r\n",
      "info =\r\n",
      "\r\n",
      "  scalar structure containing the fields:\r\n",
      "\r\n",
      "    solveiter = 4\r\n",
      "    info = 0\r\n",
      "\r\n",
      "lambda =\r\n",
      "\r\n",
      "   0.4000\r\n",
      "        0\r\n",
      "   0.2000\r\n",
      "        0\r\n",
      "\r\n"
     ]
    }
   ],
   "source": [
    "H = [14,12,0;12,21,4;0,4,6];\n",
    "q = [-1,-1,-1];\n",
    "A = [1,1,-1];\n",
    "b = [0];\n",
    "lb = [0;0;0];\n",
    "ub = [];\n",
    "x0 = [];\n",
    "[x,obj,info,lambda] = qp(x0, H, q, A, b, lb, ub)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "676b1f03",
   "metadata": {},
   "source": [
    "$a_1=0.1,a_2=0,a_3=0.1$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2df66f1a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "w =\r\n",
      "\r\n",
      "        0\r\n",
      "   0.4000\r\n",
      "   0.2000\r\n",
      "\r\n"
     ]
    }
   ],
   "source": [
    "w = x(1)*[1;2;3]*1+x(2)*[4;1;2]*1+x(3)*[-1;2;-1]*(-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "cb5b8de5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b = -0.4000\r\n"
     ]
    }
   ],
   "source": [
    "b = 1-[1,2,3]*w"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a916f873",
   "metadata": {},
   "source": [
    "超平面为: $-4+2x_1+4x_3=0$"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Octave",
   "language": "octave",
   "name": "octave"
  },
  "language_info": {
   "file_extension": ".m",
   "help_links": [
    {
     "text": "GNU Octave",
     "url": "https://www.gnu.org/software/octave/support.html"
    },
    {
     "text": "Octave Kernel",
     "url": "https://github.com/Calysto/octave_kernel"
    },
    {
     "text": "MetaKernel Magics",
     "url": "https://metakernel.readthedocs.io/en/latest/source/README.html"
    }
   ],
   "mimetype": "text/x-octave",
   "name": "octave",
   "version": "6.2.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}